package com.xj.utils;

import java.sql.Connection;
import java.util.List;
import java.util.TimerTask;
import com.xj.utils.Synchronize;
import com.xj.utils.SynchronizeImpl;
import com.xj.utils.MyInvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

import java.util.Calendar;


public class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        Calendar now = Calendar.getInstance();
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
            return;
        }
        if (!(hour >= 9 && hour < 20)) {
            return;
        }
        timerTask();
    }
    public void timerTask(){
        try{
            Connection source = MySqlConnector.getConnection("source");
            Connection target = MySqlConnector.getConnection("target");
            Synchronize synchronize = new SynchronizeImpl(source,target);
            InvocationHandler invocationHandler = new MyInvocationHandler(synchronize);
            Synchronize proxy = (Synchronize) Proxy.newProxyInstance(synchronize.getClass().getClassLoader(), synchronize.getClass().getInterfaces(), invocationHandler);
            proxy.synchronizeDB();
        } catch (Exception e) {
            System.out.println("本次同步出现异常[" + Tools.getCurrentTime() + "]:" + e.getMessage());
        }
    }
}
